package com.ruoyi.waterManagement.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.business.waterManagement.StStationR;
import com.ruoyi.common.core.domain.business.waterManagement.WrStationR;
import com.ruoyi.waterManagement.service.IWrStationRService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import static com.ruoyi.waterManagement.controller.WrStatvolHController.findDates;

/**
 * 水量在线监测站数据记录Controller
 * 
 * @author hwd
 * @date 2023-02-20
 */
@RestController
@RequestMapping("/WrStationR")
public class WrStationRController extends BaseController
{
    @Autowired
    private IWrStationRService wrStationRService;

    /**
     * 查询水量在线监测站数据记录列表
     */
    @RequiresPermissions("waterManagement:WrStationR:list")
    @GetMapping("/list")
    public TableDataInfo list(WrStationR wrStationR)
    {
        startPage();
        if (wrStationR.getChkTime()==null){
            wrStationR.setChkTime(new Date());
        }
        List<WrStationR> list = wrStationRService.selectWrStationRList(wrStationR);
        return getDataTable(list);
    }

    /**
     * 导出水量在线监测站数据记录列表
     */
    @RequiresPermissions("waterManagement:WrStationR:export")
    @Log(title = "水量在线监测站数据记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WrStationR wrStationR)
    {
        List<WrStationR> list = wrStationRService.selectWrStationRList(wrStationR);
        ExcelUtil<WrStationR> util = new ExcelUtil<WrStationR>(WrStationR.class);
        util.exportExcel(response, list, "水量在线监测站数据记录数据");
    }

    /**
     * 获取水量在线监测站数据记录详细信息
     */
    @RequiresPermissions("waterManagement:WrStationR:query")
    @GetMapping(value = "/{statCd}")
    public AjaxResult getInfo(@PathVariable("statCd") String statCd)
    {
        return success(wrStationRService.selectWrStationRByStatCd(statCd));
    }

    /**
     * 新增水量在线监测站数据记录
     */
    @RequiresPermissions("waterManagement:WrStationR:add")
    @Log(title = "水量在线监测站数据记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WrStationR wrStationR)
    {
        return toAjax(wrStationRService.insertWrStationR(wrStationR));
    }

    /**
     * 修改水量在线监测站数据记录
     */
    @RequiresPermissions("waterManagement:WrStationR:edit")
    @Log(title = "水量在线监测站数据记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WrStationR wrStationR)
    {
        return toAjax(wrStationRService.updateWrStationR(wrStationR));
    }

    /**
     * 删除水量在线监测站数据记录
     */
    @RequiresPermissions("waterManagement:WrStationR:remove")
    @Log(title = "水量在线监测站数据记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{statCds}")
    public AjaxResult remove(@PathVariable String[] statCds)
    {
        return toAjax(wrStationRService.deleteWrStationRByStatCds(statCds));
    }
    /**
     * 实时监测信息
     */
    @RequiresPermissions("waterManagement:WrStationR:list")
    @ApiOperation("测站实时监测信息过程线")
    @GetMapping("/selLineMonitoringInformationByStatCd")
    public AjaxResult selLineMonitoringInformationByStatCd(WrStationR wrStationR) throws Exception {
        if (wrStationR.getChkTime()==null){
            wrStationR.setChkTime(new Date());
        }
        List<WrStationR> channelWaterRegime = wrStationRService.selectWrStationRList(wrStationR);
        ArrayList<Object> resList = new ArrayList<>();
        //一天24条数据，每个小时出一条数据
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfQueryTime = new SimpleDateFormat("yyyy-MM-dd");
        String queryTime = sdfQueryTime.format(wrStationR.getChkTime());
        //查询测站设备上报频率
        int sb =60;
//        int time =1440/sb;
        //按频率分钟分割时间
        List<String> times =findDates("N",sb);
        for (int i = 0; i < times.size()-1 ; i++) {
            HashMap<String, Object> resMap = new HashMap<>();
            Date start = simpleDateFormat.parse(queryTime + " " + times.get(i));
            Date end = simpleDateFormat.parse(queryTime + " " + times.get(i + 1));
            List<WrStationR> collect = channelWaterRegime.stream().filter(vo -> vo.getChkTime().getTime() >= start.getTime() && vo.getChkTime().getTime() <= end.getTime())
                    .collect(Collectors.toList());
            if (collect.size() <= 0) {
                resMap.put("statHour", end);
                resMap.put("hourFlow", "");

            } else {
                resMap.put("statHour", end);
                resMap.put("hourFlow", collect.get(0).getCurFlow() == null ? "" : collect.get(0).getCurFlow());
            }
            resList.add(resMap);
        }
        return AjaxResult.success(resList);
    }

}
